package com.scalar.chiptrack.sales;

import com.scalar.chiptrack.commons.Address;

/**
 * 
 * Author  : rohan
 * Date    : May 11, 2004
 * Version : 1.0 
 */

/**
 * This class contains utility method required while integrating Sales Order with Epicore
 *
 */
public class EPISalesOrderUtils
{
    /**
     * This method will set the sales order number as the combinations of sales order number and sales order release number
     * seperated by a hyphen.
     *
     * @param salesOrder
     */
    public static void setChiptracSalesOrderNumber( SalesOrder salesOrder )
    {
        salesOrder.setSalesOrderNo( salesOrder.getSalesOrderNo().trim() + "-" + salesOrder.getSalesOrderReleaseNumber() );
    }

    /**
     * This method will retreive the sales order number and the release number from the sales order number
     * and sets it to the sales order.
     *
     * @param salesOrder
     */
    public static void setEpicoreSalesOrderNumber( SalesOrder salesOrder )
    {
        String salesOrderNumber = salesOrder.getSalesOrderNo();
        if( salesOrderNumber.indexOf( "-" ) == -1 )
        {
            //if there is no hyphen in the sales order number then keeping
            //the sales order number as it is and setting the release number as 0
            salesOrder.setSalesOrderReleaseNumber( 0 );
        }
        else
        {
            salesOrder.setSalesOrderNo( salesOrderNumber.substring( 0, salesOrderNumber.indexOf( "-" ) ) );
            salesOrder.setSalesOrderReleaseNumber( Integer.parseInt( salesOrderNumber.substring( salesOrderNumber.indexOf( "-" ) + 1, salesOrderNumber.length() ) ) );
        }
    }

    /**
     * This method whill return the epicore Sales Order number from the Chiptrac Sales Order number
     *
     * @param salesOrderNumber
     */
    public static String retreiveEpicoreSalesOrderNumber( String salesOrderNumber )
    {
        String epiSalesOrderNumber = "";

        if( salesOrderNumber.indexOf( "-" ) == -1 )
        {
            //if there is no hyphen in the sales order number then returning
            //the same sales order number
            epiSalesOrderNumber = salesOrderNumber;
        }
        else
        {
            epiSalesOrderNumber  = salesOrderNumber.substring( 0, salesOrderNumber.indexOf( "-" ) ) ;
        }

        return epiSalesOrderNumber;
    }

    /**
     * This method whill return the epicore Sales Order release number from the Chiptrac Sales Order number
     *
     * @param salesOrderNumber
     */
    public static int retreiveEpicoreSalesOrderReleaseNumber( String  salesOrderNumber )
    {
        int epiReleaseNumber = 0;

        if( salesOrderNumber.indexOf( "-" ) != -1 )
        {
            epiReleaseNumber = Integer.parseInt( salesOrderNumber.substring( salesOrderNumber.indexOf( "-" ) + 1, salesOrderNumber.length() ) );
        }

        return epiReleaseNumber;
    }

    /**
     * This method will prepare the given address object for inserting into the Epicore database by truncating
     * those fields which are more than the maximum allowable length in Epicore
     *
     * @param address
     */
    public static void prepareAddressForEpicoreInsertion( Address address )
    {
        if( address.getAddress1() == null )
        {
            address.setAddress1( "" );
        }
        else if( address.getAddress1().trim().length() > 40 )
        {
            address.setAddress1( address.getAddress1().trim().substring( 0 , 39 ) );
        }

        if( address.getAddress2() == null )
        {
            address.setAddress2( "" );
        }
        else if( address.getAddress2().trim().length() > 40 )
        {
            address.setAddress2( address.getAddress2().trim().substring( 0 , 39 ) );
        }

        if( address.getAddress3() == null )
        {
            address.setAddress3( "" );
        }
        else if( address.getAddress3().trim().length() > 40 )
        {
            address.setAddress3( address.getAddress3().trim().substring( 0 , 39 ) );
        }

        if( address.getAddress4() == null )
        {
            address.setAddress4( "" );
        }
        else if( address.getAddress4().trim().length() > 40 )
        {
            address.setAddress4( address.getAddress4().trim().substring( 0 , 39 ) );
        }

        if( address.getCity() == null )
        {
            address.setCity( "" );
        }
        else if( address.getCity().trim().length() > 40 )
        {
            address.setCity( address.getCity().trim().substring( 0 , 39 ) );
        }

        if( address.getState() == null )
        {
            address.setState( "" );
        }
        else if( address.getState().trim().length() > 2 )
        {
            address.setState( address.getState().trim().substring( 0 , 1 ) );
        }

        if( address.getZipCode() == null )
        {
            address.setZipCode( "" );
        }
        else if( address.getZipCode().trim().length() > 10 )
        {
            address.setZipCode( address.getZipCode().trim().substring( 0 , 9 ) );
        }

        if( address.getCountry() == null )
        {
            address.setCountry( "" );
        }
        else if( address.getCountry().trim().length() > 40 )
        {
            address.setCountry( address.getCountry().trim().substring( 0 , 39 ) );
        }
    }

    /**
     * This method will prepare the given address object for inserting into the CT database by truncating
     * those fields which are more than the maximum allowable length in CT
     *
     * @param address
     */
    public static void prepareAddressForChiptracInsertion( Address address )
    {
        if( address.getAddress1() == null )
        {
            address.setAddress1( "" );
        }
        else if( address.getAddress1().trim().length() > 50 )
        {
            address.setAddress1( address.getAddress1().trim().substring( 0 , 49 ) );
        }

        if( address.getAddress2() == null )
        {
            address.setAddress2( "" );
        }
        else if( address.getAddress2().trim().length() > 50 )
        {
            address.setAddress2( address.getAddress2().trim().substring( 0 , 49 ) );
        }

        if( address.getAddress3() == null )
        {
            address.setAddress3( "" );
        }
        else if( address.getAddress3().trim().length() > 50 )
        {
            address.setAddress3( address.getAddress3().trim().substring( 0 , 49 ) );
        }

        if( address.getAddress4() == null )
        {
            address.setAddress4( "" );
        }
        else if( address.getAddress4().trim().length() > 50 )
        {
            address.setAddress4( address.getAddress4().trim().substring( 0 , 49 ) );
        }

        if( address.getCity() == null )
        {
            address.setCity( "" );
        }
        else if( address.getCity().trim().length() > 50 )
        {
            address.setCity( address.getCity().trim().substring( 0 , 49 ) );
        }

        if( address.getState() == null )
        {
            address.setState( "" );
        }
        else if( address.getState().trim().length() > 50 )
        {
            address.setState( address.getState().trim().substring( 0 , 49 ) );
        }

        if( address.getZipCode() == null )
        {
            address.setZipCode( "" );
        }
        else if( address.getZipCode().trim().length() > 50 )
        {
            address.setZipCode( address.getZipCode().trim().substring( 0 , 49 ) );
        }

        if( address.getCountry() == null )
        {
            address.setCountry( "" );
        }
        else if( address.getCountry().trim().length() > 50 )
        {
            address.setCountry( address.getCountry().trim().substring( 0 , 49 ) );
        }

        if( address.getPhoneNumber() == null )
        {
            address.setPhoneNumber( "" );
        }
        else if( address.getPhoneNumber().trim().length() > 50 )
        {
            address.setPhoneNumber( address.getPhoneNumber().trim().substring( 0 , 49 ) );
        }

        if( address.getFaxNumber() == null )
        {
            address.setFaxNumber( "" );
        }
        else if( address.getFaxNumber().trim().length() > 50 )
        {
            address.setFaxNumber( address.getFaxNumber().trim().substring( 0 , 49 ) );
        }

        if( address.getEmail() == null )
        {
            address.setEmail( "" );
        }
        else if( address.getEmail().trim().length() > 50 )
        {
            address.setEmail( address.getEmail().trim().substring( 0 , 49 ) );
        }

        if( address.getContactPerson() == null )
        {
            address.setContactPerson( "" );
        }
        else if( address.getContactPerson().trim().length() > 50 )
        {
            address.setContactPerson( address.getContactPerson().trim().substring( 0 , 49 ) );
        }

        if( address.getCellNumber() == null )
        {
            address.setCellNumber( "" );
        }
        else if( address.getCellNumber().trim().length() > 50 )
        {
            address.setCellNumber( address.getCellNumber().trim().substring( 0 , 49 ) );
        }

        if( address.getEpicoreShipCode() == null )
        {
            address.setEpicoreShipCode( "" );
        }
        else if( address.getEpicoreShipCode().trim().length() > 8 )
        {
            address.setEpicoreShipCode( address.getEpicoreShipCode().trim().substring( 0 , 7 ) );
        }

    }

    /**
     * This method will prepare the given sales order object for inserting into the CT database by truncating
     * those fields which are more than the maximum allowable length in CT
     *
     * @param salesOrder
     */
    public static void prepareSalesOrderForChiptracInsertion( SalesOrder salesOrder )
    {

        //preparing shipping address and billing address objects
        prepareAddressForChiptracInsertion( salesOrder.getShippingAddressAsObject());
        prepareAddressForChiptracInsertion( salesOrder.getBillingAddressAsObject());

        if( salesOrder.getSalesOrderNo() == null )
        {
            salesOrder.setSalesOrderNo( "" );
        }
        else if( salesOrder.getSalesOrderNo().trim().length() > 50 )
        {
            salesOrder.setSalesOrderNo( salesOrder.getSalesOrderNo().trim().substring( 0, 49 ) );
        }

        if( salesOrder.getCustomerPO() == null )
        {
            salesOrder.setCustomerPO( "" );
        }
        else if( salesOrder.getCustomerPO().trim().length() > 50 )
        {
            salesOrder.setCustomerPO( salesOrder.getCustomerPO().trim().substring( 0, 49 ) );
        }

        if( salesOrder.getContactPerson() == null )
        {
            salesOrder.setContactPerson( "" );
        }
        else if( salesOrder.getContactPerson().trim().length() > 50 )
        {
            salesOrder.setContactPerson( salesOrder.getContactPerson().trim().substring( 0, 49 ) );
        }

        if( salesOrder.getSalesPerson() == null )
        {
            salesOrder.setSalesPerson( "" );
        }
        else if( salesOrder.getSalesPerson().trim().length() > 50 )
        {
            salesOrder.setSalesPerson( salesOrder.getSalesPerson().trim().substring( 0, 49 ) );
        }

        if( salesOrder.getShippingMethodName() == null )
        {
            salesOrder.setShippingMethodName( "" );
        }
        else if( salesOrder.getShippingMethodName().trim().length() > 50 )
        {
            salesOrder.setShippingMethodName( salesOrder.getShippingMethodName().trim().substring( 0, 49 ) );
        }

        if( salesOrder.getSalesTaxType() == null )
        {
            salesOrder.setSalesTaxType( "" );
        }
        else if( salesOrder.getSalesTaxType().trim().length() > 50 )
        {
            salesOrder.setSalesTaxType( salesOrder.getSalesTaxType().trim().substring( 0, 49 ) );
        }

        if( salesOrder.getEpicoreSalesOrderStatus() == null )
        {
            salesOrder.setEpicoreSalesOrderStatus( "" );
        }
        else if( salesOrder.getEpicoreSalesOrderStatus().trim().length() > 1 )
        {
            salesOrder.setEpicoreSalesOrderStatus( salesOrder.getEpicoreSalesOrderStatus().trim().substring( 0, 0 ) );
        }
    }

    /**
     * This method will prepare the given line item object for inserting into the CT database by truncating
     * those fields which are more than the maximum allowable length in CT
     *
     * @param lineItem
     */
    public static void prepareLineItemForChiptracInsertion( LineItem lineItem )
    {

        if( lineItem.getDescription() == null )
        {
            lineItem.setDescription( "" );
        }
        else if( lineItem.getDescription().trim().length() > 200 )
        {
            lineItem.setDescription( lineItem.getDescription().trim().substring( 0, 199 ) );
        }

        if( lineItem.getLineItemDescription() == null )
        {
            lineItem.setLineItemDescription( "" );
        }
        else if( lineItem.getLineItemDescription().trim().length() > 200 )
        {
            lineItem.setLineItemDescription( lineItem.getLineItemDescription().trim().substring( 0, 199 ) );
        }
    }
}
